package com.app.mapper;

import com.app.model.ActivityModel;
import com.app.model.BusinessModel;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 商家信息
 *
 * @author 16693103420
 * @since 2024-01-22
 */
@Mapper
public interface BusinessMapper extends BaseMapper<BusinessModel> {

    //查询此用户收藏的商家
    @Select("select b.*," +
            "ROUND(st_distance(point(b.longitude, b.latitude), point(#{longitude}, #{latitude})) * 111.195,2) AS distance " +
            "from tb_business b,tmp_user_business tmp,tb_user u " +
            "where " +
            "b.id=tmp.business_id " +
            "and u.id=tmp.user_id " +
            "and u.id=#{userId} " +
            "order by b.create_time desc")
    IPage<BusinessModel> getUserBusiness(IPage<BusinessModel> page,
                                         @Param("userId") Integer userId,
                                         @Param("longitude") Double longitude,
                                         @Param("latitude") Double latitude
    );

    Map<String, Integer> accessToBusinessData(@Param("userId") Integer userId);

    Map<String, Integer> selectViewCount(@Param("userId") Integer userId);

    /**
     * 根据手机号查询商家的状态和商家管理表的id
     *
     * @param userPhone
     * @return
     */
    @Select("select id,status from tb_business where user_phone = #{userPhone}")
    BusinessModel selectByUserPhone(String userPhone);

    /**
     * 入驻或是退出入驻
     *
     * @param status 1入驻 0未入驻
     * @param id
     */
    @Update("update tb_business set status = #{status} where id = #{id}")
    void updateStatus(Integer status, Integer id);

    @Select("select id,status from tb_business where user_id = #{userId}")
    BusinessModel selectByUserId(Integer userId);

    /**
     * 分页查询关联区县代理
     *
     * @param offset
     * @param limit
     * @param keywords
     * @param areaId
     * @param status
     * @return
     */
    List<BusinessModel> selectPageTwoTables(@Param("offset") int offset, @Param("limit") Integer limit, @Param("keywords") String keywords, @Param("areaId") Integer areaId, @Param("status") Integer status);

    List<Integer> selectBusinessIdsByClassification(@Param("classification") Integer classification);

    IPage<ActivityModel> getActivityList(IPage<ActivityModel> pa,
                                         @Param("classification") Integer classification,
                                         @Param("activityName") String activityName,
                                         @Param("type") Integer type,
                                         @Param("sort") Integer sort);

    Page<BusinessModel> queryMerchantsForASpecificActivity(Page<BusinessModel> page1,
                                                           @Param("classification") Integer classification,
                                                           @Param("name") String name,
                                                           @Param("type") Integer type,
                                                           @Param("longitude") Double longitude,
                                                           @Param("latitude") Double latitude
    );

    Page<BusinessModel> getAllBusinessList(Page<BusinessModel> page1,
                                           @Param("name") String businessName,
                                           @Param("type") Integer type,
                                           @Param("longitude") Double longitude,
                                           @Param("latitude") Double latitude
                                           );

    @Select("select * from tb_business where user_phone = #{userPhone}")
    BusinessModel selectAllByUserPhone(String userPhone);

    IPage<BusinessModel> pagingQuery(Page<BusinessModel> businessModelPage,
                                     @Param("businessName") String businessName,
                                     @Param("isPick") Integer isPick,
                                     @Param("type") Integer type,
                                     @Param("areaId") Integer areaId,
                                     @Param("longitude") Double longitude,
                                     @Param("latitude") Double latitude);

    /**
     * 按业务id查询列表
     *
     * @param status         状态
     * @param businessStatus 业务状态
     * @param businessIds    业务id
     * @return {@link List}<{@link BusinessModel}>
     */
    List<BusinessModel> queryListByBusinessIds(@Param("status") Integer status,
                                               @Param("businessStatus") Integer businessStatus,
                                               @Param("businessIds") List<String> businessIds,
                                               @Param("longitude") Double longitude,
                                               @Param("latitude") Double latitude);

    BusinessModel details(@Param("id") Integer id,
                          @Param("longitude") Double longitude,
                          @Param("latitude") Double latitude);
}
